package cn.com.platform.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.easy.exception.BusinessException;
import cn.com.easy.permission.model.UserModel;
import cn.com.platform.dao.AdvItemDao;
import cn.com.platform.entity.AdvertItemEntity;
import cn.com.platform.web.manage.dto.AdvItemDto;

/***
 * 广告服务类
 * 
 * @author chenbinghuang 2017年6月14日
 * 
 */
@Service
public class AdvService {
	/** 广告 */
	@Autowired
	private AdvItemDao advItemDao;

	public List<AdvertItemEntity> getItemList(Long cityId, Long Id) {
		List<AdvertItemEntity> list = null;
		if (cityId == null) {
			list = advItemDao.getItemList(Id);

		} else {
			list = advItemDao.getItemList(cityId, Id);
			if (list.size() == 0) {
				// 本地没有取全局的
				list = advItemDao.getItemList(Id);
			}
		}
		return list;
	}

	/***
	 * 保存或修改广告
	 * 
	 * @param advItemDto
	 * @param admin
	 * @throws BusinessException
	 * @author chenbinghuang 2017年6月14日
	 */
	public void saveOrUpdate(AdvItemDto advItemDto, UserModel admin) throws BusinessException {

		if (advItemDto.getCityId() == null || advItemDto.getCityId() < -1 || advItemDto.getCityId() == null || advItemDto.getCityId() < -1) {
			throw new BusinessException("城市不能为空");
		}
		if (StringUtils.isEmpty(advItemDto.getTitle())) {
			throw new BusinessException("标题不能为空");
		}
		if (advItemDto.getOrderby() <= 0) {
			throw new BusinessException("排序不能小于0");
		}
		if (advItemDto.getId() == null) {
			if (StringUtils.isEmpty(advItemDto.getPhoto())) {
				throw new BusinessException("广告图不能为空");
			}
			AdvertItemEntity advItemEntityTemp = new AdvertItemEntity();
			advItemEntityTemp.setId(advItemDto.getId());
			advItemEntityTemp.setProvinceId(advItemDto.getProvinceId());
			advItemEntityTemp.setDescription(advItemDto.getDescription());
			advItemEntityTemp.setAdvId(advItemDto.getAdvId());
			advItemEntityTemp.setTitle(advItemDto.getTitle());
			advItemEntityTemp.setCityId(advItemDto.getCityId());
			advItemEntityTemp.setLink(advItemDto.getLink());
			advItemEntityTemp.setClosed(advItemDto.isClosed());
			if (StringUtils.isNoneBlank(advItemDto.getPhoto()) == true) {
				advItemEntityTemp.setPhoto(advItemDto.getPhoto());
			}
			advItemEntityTemp.setStartTime(advItemDto.getStartTime());
			advItemEntityTemp.setEndTime(advItemDto.getEndTime());
			advItemEntityTemp.setAudit(advItemDto.getAudit());
			advItemEntityTemp.setOrderby(advItemDto.getOrderby());
			advItemEntityTemp.setCreateBy(admin.getLoginname());
			advItemDao.save(advItemEntityTemp);
		} else {
			AdvertItemEntity advItemEntityTemp = advItemDao.findOne(advItemDto.getId());
			if (advItemEntityTemp == null) {
				throw new BusinessException("广告id不存在");
			}
			advItemEntityTemp.setId(advItemDto.getId());
			advItemEntityTemp.setProvinceId(advItemDto.getProvinceId());
			advItemEntityTemp.setTitle(advItemDto.getTitle());
			advItemEntityTemp.setCityId(advItemDto.getCityId());
			advItemEntityTemp.setLink(advItemDto.getLink());
			advItemEntityTemp.setDescription(advItemDto.getDescription());
			if (StringUtils.isNotBlank(advItemDto.getPhoto())) {
				advItemEntityTemp.setPhoto(advItemDto.getPhoto());
			}
			advItemEntityTemp.setStartTime(advItemDto.getStartTime());
			advItemEntityTemp.setEndTime(advItemDto.getEndTime());
			advItemEntityTemp.setAudit(advItemDto.getAudit());
			advItemEntityTemp.setOrderby(advItemDto.getOrderby());
			advItemEntityTemp.setLastModifyBy(admin.getLoginname());
			advItemEntityTemp.setLastModifyTime(new Date());
			advItemDao.save(advItemEntityTemp);
		}
	}

}
